package com.link.base.base.consumer.controller;

import com.link.base.base.consumer.model.PortalAccntLine;
import com.link.base.base.consumer.service.PortalAccntLineService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.modules.export.ExportFactory;
import com.link.core.modules.export.ExportWriter;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户信息相关服务
 * @author yipeng.zhu@hand-china.com
 * @version 1.0
 * @date: 2018/11/2 15:11
 */

@Controller
@RequestMapping("/link/accntLine")
public class AccntLineController extends BasicController<PortalAccntLine> {
    @Resource
    private PortalAccntLineService portalAccntLineService;
    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicService<PortalAccntLine> getBasicService() throws Exception {
        return portalAccntLineService;
    }

    @Override
    public void beforQueryExample(PortalAccntLine entity, HttpServletRequest request) throws Exception {

        entity.setAcctType("ChannelConsumer");
        entity.setAcctChannel("OFFLINE");
    }

    @Override
    public void beforUpsert(PortalAccntLine entity, HttpServletRequest request) throws Exception {
        try {
            CoreUser landedUser = UserUtil.getUser();
            if (landedUser == null) {
                entity.setId(0L);
            } else {
                entity.setOrgId(landedUser.getOrgId());
            }
            entity.setAcctChannel("OFFLINE");
        } catch (Exception e) {
            LCLogger.withException(e);
        }
    }

    /**
     * 导出消费者模板
     * @author      yipeng.zhu@hand-china.com
     * @param       entity
     * @return
     * @exception
     * @date        2019/3/7 20:49
     * @version     1.0
     */
    @Override
    @RequestMapping(value = "/exportTemplet")
    @ResponseBody
    public Map<String, Object> exportData(@JsonParam PortalAccntLine entity,@JsonParam QueryParams qps, HttpSession session,
                                          HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            ExportWriter exportWriter;
            // 解析xlsx
            exportWriter = ExportFactory.Excel2007Instance(qps);
            PortalAccntLine t = (PortalAccntLine) BasicModel.transformClass(entity, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<PortalAccntLine> list = null;
            if (qps.getPage() == 1) {
                qps.setTotal(t.getTotal());
            }
            exportWriter.wirteData(list);
            t.setPage(qps.getPage());
            t.setTotalFlag(qps.getTotalFlag());

            result.put("success", true);
            result.put("url", exportWriter.getUrlPath());
        } catch (ServiceException e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
            result.put("detailMessage", e.getMessage());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 插入 消费者，消费者地址，消费者标签
     * @author yipeng.zhu@hand-china.com
     * @param accntline 消费者
     * @param qps
     * @param session
     * @param request
     * @param response
     * @return
     * @time 2018-5-19
     */
    @RequestMapping(value = "/moreModelInsert")
    @ResponseBody
    public Map<String, Object> moreModelInsert(@JsonParam PortalAccntLine accntline,@JsonParam QueryParams qps, HttpSession session,
                                               HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            accntline.setId(keyGenerateService.keyGenerate());
            portalAccntLineService.moreModelInsert(accntline);
            result.put("success", true);
            result.put("accntId", accntline.getId());
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
        }
        return result;
    }
}
